/* eslint-disable */
const { Controller } = require('egg');

class CaptchaController extends Controller {
    //生成验证码
    async generateCaptcha() {
        const { ctx } = this;
        const { username } = ctx.request.body;

        if (!username) {
            ctx.error('请先输入账号');
            return;
        }
        try {
            const captcha = await ctx.service.captcha.generateCode(username);
            ctx.success(captcha, '验证码生成成功');
        } catch (error) {
            ctx.error(error.message || '验证码生成失败');
        }
    }

    // 验证用户输入的验证码
    async validateCaptcha() {
        const { ctx } = this;
        const { username, answer } = ctx.request.body;

        try {
            await ctx.service.captcha.validateCode(username, answer);
            ctx.success({}, '验证码验证成功');
        } catch (error) {
            ctx.error(error.message || '验证码验证失败');
        }
    }

}
module.exports = CaptchaController